package gl.android.biyiqing.fragment;

import gl.android.biyiqing.R;
import gl.android.biyiqing.api.API;
import gl.android.biyiqing.competent.OnGreeItemClickListener;
import gl.common.UserManger;

import org.json.JSONException;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 两人对话
 * 
 * @author geliang
 * 
 */
public class GreetFragment extends BaseListFragmet {

	private TextView empty;
	private ListView list;
	protected String NOTFOUND = "似乎你们还没聊过";
	private Drawable drawable;
	private String sex;

	private void bindViews() {
		empty = (TextView) findViewById(R.id.empty);
		list = (ListView) findViewById(android.R.id.list);
		list.setEmptyView(empty);
		empty.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				initdata();
			}
		});
		empty.setText(NOTFOUND);
		empty.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null,
				null);
	}

	public GreetFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.listview_empty, container,
				false);
		drawable = getActivity().getResources().getDrawable(
				R.drawable.ic_talking_btn_mail_p);
		bindViews();
		Bundle pars = getArguments();
		if (pars != null) {

			if (UserManger.getInstance().userinfo != null) {
				sex = UserManger.getInstance().userinfo.getGender() + "";
			}
			if (TextUtils.isEmpty(sex)) {
				sex = "1";
			}
			initdata();
		}
		return rootView;
	}

	private void initdata() {
		initAbsListView(getListView(), API.URL + "GetGreeList?sex=%s",
				new Object[] { sex }, R.layout.item_list_gree,
				new String[] { "greeting" }, new int[] { R.id.tv_nick });

	}
	@Override
	public Object getOnItemListener(String[] from, String t)
			throws JSONException {
		return new OnGreeItemClickListener(t, getArguments().getString(
				KEY_TO_USER_ID));
	}
}